package com.wnhoo.smartfishtank.app;

import android.app.Application;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wnhoo.smartfishtank.api.data.DataPlug;
import com.wnhoo.smartfishtank.contants.KContants;
import com.wnhoo.smartfishtank.util.MyLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LMAPP extends Application {
    private static final String tag = "PlugAPP";
    private static HashMap<String, String> device_ip;
    public static String token_default ;
    public static SharedPreferences sharedPreferences ;
    public static List<DataPlug> data_plug_list;
    public static DataPlug dataPlug;
    private static LMAPP myApplication;
    public static ExecutorService exec;
    @Override
    public void onCreate() {
        super.onCreate();
        sharedPreferences = getSharedPreferences(KContants.SPF_PLUG_DATA, MODE_PRIVATE);
        token_default = null;
        device_ip = new HashMap<String, String>();
        data_plug_list = new ArrayList<>();
        dataPlug = new DataPlug();
        getAccessToken();
        loadPlugList();
        exec = Executors.newCachedThreadPool();
    }
    public static LMAPP getInstance() {
        if (myApplication == null) {
            myApplication = new LMAPP();
        }
        return myApplication;
    }
    public static String  getAccessToken(){
        if(token_default==null){
            token_default = sharedPreferences.getString("access_token",null);
            if(token_default!=null&&token_default.length()<5){
                token_default = null;
            }
        }
        return token_default;
    }
    public static void writeSPFAccessToken(String token){

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("access_token", token);
        editor.apply();
    }

    /**
     * 在内存中搜索Plug
     *
     * @param device_id
     * @return 存在Plug就返回DataPlug，否则返回null
     */
    public static DataPlug searchPlug(String device_id) {

        if (device_id == null) return null;
        for (DataPlug temp : data_plug_list) {
            Log.i(tag, "temp.getId().equals(device_id)" + temp.getId().equals(device_id));
            if (temp.getId().equals(device_id)) {
                return temp;
            }
        }
        return null;
    }
    /**
     * 添加一个Plug到SharedPreferences
     *
     * @param dataPlug
     * @return 如果设备已存在，返回false
     */
    public static boolean addPlug(DataPlug dataPlug) {

        //已存在的问题
        if (searchPlug(dataPlug.getId()) != null) return false;

        data_plug_list.add(dataPlug);
        if (LMAPP.dataPlug == null) {
            LMAPP.dataPlug = dataPlug;
        }
        saveMemoryPlugToSPF();
        return true;
    }
    /**
     * 删除一个Plug到SharedPreferences
     *
     * @param dataPlug
     * @return 如果设备已存在，返回false
     */
    public static boolean deletePlug(DataPlug dataPlug) {
        Log.i(tag, "删除设备" + dataPlug.getId());
//        已存在的问题
//        if (searchPlug(dataPlug.getId()) != null) return false;
        data_plug_list.remove(dataPlug);
        saveMemoryPlugToSPF();
        return true;
    }
    /**
     * To save the memory data to SharedPreferences
     */
    public static void saveMemoryPlugToSPF() {
        Gson gson = new Gson();
        String plug_list_json = gson.toJson(data_plug_list);
        MyLog.e(tag, "plug_list_json=" + plug_list_json);
        //System.out﹕ plug_list_json=[{"about":"Plug","id":"1005ACCF2337CB14","image":"null","title":"插座CB14","type":10015},{"about":"Plug","id":"1005ACCF2337CB15","image":"null","title":"插座CB15","type":10015},{"about":"Plug","id":"1005ACCF2337CB16","image":"null","title":"插座CB16","type":10015}]
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("plug_list_json", plug_list_json);
        editor.apply();
    }
    /**
     * 初始化,将存储在SharedPreferences的Plug List全部加载到内存中
     */
    public static void loadPlugList() {

        if (data_plug_list == null) {
            data_plug_list = new ArrayList<DataPlug>();
        }
        String plug_list_json_spf = sharedPreferences.getString("plug_list_json", null);
        if (plug_list_json_spf == null || "".equals(plug_list_json_spf)) return;
        Gson gson = new Gson();
        Log.e(tag, "load plug_list_json_spf=" + plug_list_json_spf);

        java.lang.reflect.Type type = new TypeToken<List<DataPlug>>() {
        }.getType();
        //MyLog.s(tag, "debug12");
        data_plug_list = gson.fromJson(plug_list_json_spf, type);

        Log.e(tag, "load plug_list.size()=" + data_plug_list.size());
        if (!data_plug_list.isEmpty()) {
            dataPlug = data_plug_list.get(0);
        } else {
            dataPlug = null;
        }
    }

}
